package com.maimang.platform.dsp.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maimang.platform.dsp.constant.AdvertiserApi;
import com.maimang.platform.dsp.constant.ParamFields;
import com.maimang.platform.dsp.entity.AdvertiserIds;
import com.maimang.platform.dsp.entity.Campaign;
import com.maimang.platform.dsp.mapper.CampaignMapper;
import com.maimang.platform.dsp.service.AccessTokenService;
import com.maimang.platform.dsp.service.AdvertiserIdsService;
import com.maimang.platform.dsp.service.CampaignService;
import com.maimang.platform.dsp.utils.ApiService;
import com.maimang.platform.lang.ConvertUtils;
import com.maimang.platform.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname CampaignServiceImpl
 * @description <br>广告组服务层实现类</br>
 * @date 2019-11-25 11:13:45
 * @email jintaozhao@qq.com
 */
@Service
public class CampaignServiceImpl implements CampaignService {

	@Autowired
	private CampaignMapper campaignMapper;

	@Autowired
	private ApiService apiService;

	@Autowired
	private AdvertiserIdsService advertiserIdsService;

	@Autowired
	private AccessTokenService accessTokenService;

	/**
	 * 查询广告组信息
	 *
	 * @param id 广告组ID
	 * @return 广告组信息
	 */
	@Override
	public Campaign selectCampaignById(Long id) {
		return campaignMapper.selectCampaignById(id);
	}

	/**
	 * 查询广告组列表
	 *
	 * @param campaign 广告组信息
	 * @return 广告组集合
	 */
	@Override
	public List<Campaign> selectCampaignList(Campaign campaign) {
		return campaignMapper.selectCampaignList(campaign);
	}

	/**
	 * 新增广告组
	 *
	 * @param campaign 广告组信息
	 * @return 结果
	 */
	@Override
	public int insertCampaign(Campaign campaign) {
		return campaignMapper.insertCampaign(campaign);
	}

	/**
	 * 修改广告组
	 *
	 * @param campaign 广告组信息
	 * @return 结果
	 */
	@Override
	public int updateCampaign(Campaign campaign) {
		return campaignMapper.updateCampaign(campaign);
	}

	/**
	 * 删除广告组对象
	 *
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteCampaignByIds(String ids) {
		return campaignMapper.deleteCampaignByIds(ConvertUtils.toStrArray(ids));
	}

	/**
	 * 获取广告组信息
	 *
	 * @param advertiserId 广告主id
	 * @param page         页数
	 * @param pageSize     每页尺寸
	 */
	@Override
	public void getCampagin(Long advertiserId, int page, int pageSize) throws Exception {
		Map param = new HashMap();
		if (StringUtils.isNotNull(advertiserId)) {
			param.put("advertiser_id" , advertiserId);
		}
		if (page >= 0) {
			param.put("page" , page);
		}
		if (pageSize > 0) {
			param.put("page_size" , pageSize);
		}
		param.put("fields" , ParamFields.CAMPAIGN_INFO);

		String accessToken = accessTokenService.accessToken();
		JSONObject result = apiService.sendGet(AdvertiserApi.CAMPAIGN_GET, accessToken, param);
		if (result != null) {
			if (result.getInteger("code") == 0) {
				JSONObject data = result.getJSONObject("data");
				if (data != null) {
					JSONArray list = data.getJSONArray("list");
					List<Campaign> campaignList = list.toJavaList(Campaign.class);
					if (campaignList.size() > 0) {
						for (int i = 0; i < campaignList.size(); i++) {
							Campaign campaign = campaignList.get(i);
							campaign.setAdvertiserId(advertiserId);
							// 删除原来的旧数据，减少查询
							if (StringUtils.isNotNull(campaign.getId())) {
								campaignMapper.deleteCampaignById(campaign.getId());
								// 重新插入新的数据
								insertCampaign(campaign);
							}
						}
					}
					JSONObject pageInfo = data.getJSONObject("page_info");
					if (StringUtils.isNotNull(pageInfo)) {
						int current_page = pageInfo.getIntValue("page");
						int page_size = pageInfo.getIntValue("page_size");
						int total_page = pageInfo.getIntValue("total_page");
						if (current_page < total_page) {
							Thread.sleep(1000);
							getCampagin(advertiserId, current_page + 1, page_size);
						}
					}
				}
			}
		}
	}

}
